package com.hebeu.device.controller;

import com.github.pagehelper.PageInfo;
import com.hebeu.device.pojo.DUser;
import com.hebeu.device.pojo.DUserRole;
import com.hebeu.device.service.LoginService;
import com.hebeu.device.service.RoleService;
import com.hebeu.device.service.UserService;
import com.hebeu.device.utils.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private LoginService loginService;
    @Resource
    UserService userService;
    @Resource
    RoleService roleService;

    @PostMapping("/getUser")
    public DUser getUser(String userName, String password, HttpSession session){
        System.out.println(userName + ":" + password);
        //通过用户名密码查询用户信息
        DUser dUser = loginService.getDUser(userName, password);
        session.setAttribute("user", dUser);
        return dUser;
    }

    //分页，搜索
    @RequestMapping("/list")
    public PageInfo<DUser> getDUser(DUser dUser){
        System.out.println("参数信息："+dUser.toString());
        List<DUser> dUsers = userService.getDUser(dUser);
        PageInfo<DUser> pageInfo = new PageInfo<>(dUsers);
        return pageInfo;
    }


    //是否有效查询角色类型，用于用户添加和修改
//    @RequestMapping("/getRoleforUser")
//    public Map<String,List<?>> getRoleforUser(){
//        Map<String,List<?>> map = new HashMap<>();
//        DUserRole dUserRole = new DUserRole();
//        dUserRole.setIsEffectice(1);
//        System.out.println("进入getRoleforUser方法");
//        map.put("roles",roleService.getAllRole(dUserRole));
//        return map;
//    }
    //通过id查询，在修改框里显示当前数据（展示某一个）
    @RequestMapping("/getOne")
    public Object getUserById(String uId){
        System.out.println("进入getOne方法");

        // 创建map，用于返回多种数据
        Map<String, Object> map = new HashMap<>();
        //通过id查用户
        DUser dUser = userService.getUserById(uId);

        DUserRole dUserRole = new DUserRole();
        dUserRole.setIsEffectice(1);
        roleService.getAllRole(dUserRole);

        System.out.println("进入getRoleforUser方法");
        map.put("roles",dUserRole);
        map.put("users",dUser);
        return map;
    }



    /**
     * 查询用户信息
     * @param httpSession
     * @return
     */
    @GetMapping("/userinfo")
    public DUser getUserInfo(HttpSession httpSession) {
        DUser user = (DUser) httpSession.getAttribute("user");
        return user;
    }
    //获取所有用户
    @GetMapping("/getAll")
    public List<DUser> getAllDuser(DUser dUser) {
    return userService.getAllDuser(dUser);
    }




//添加用户
    @PostMapping("/addUser")
    public int addDUser(DUser dUser){
        String randomStringByLength = StringUtils.getRandomStringByLength(10);
        dUser.setuId(randomStringByLength);
        System.out.println("进入添加用户方法");
        return userService.addDUser(dUser);
    }

    //修改用户信息
    @PostMapping("/updateUser")
    public DUser updateDUser(DUser dUser){
        userService.updateDUser(dUser);
        DUser userById = userService.getUserById(dUser.getuId());
        return userById;
    }

    //删除用户
    @PostMapping("/delUser")
    public int delDUser(DUser dUser){
        System.out.println("进入修改失效方法");
        return userService.delDUser(dUser);

    }


     //修改密码

    @RequestMapping("/updatePwd")
    @ResponseBody
    public Object updatePassWord(String pass,String newpass,String username,HttpSession session){
        Map<String,Object> map=new HashMap<>();
        DUser dUser = (DUser) session.getAttribute("user");
        int i= (int) userService.modifyPassword( pass, newpass, username, session);
        if(i==1){
            map.put("code",1);
            map.put("msg","修改成功！对 ");

        }else {
            map.put("code",0);
            map.put("msg","修改失败了呀啊啊啊");
        }
        return map;
    }
}
